package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.dto.join.account.group.request.SendJoinAccountGroupRequestDto
import com.opennews.openplatform.familyexpenseapi.service.JoinAccountGroupRequestService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto
import com.opennews.openplatform.myspringbootcore.security.MyClaims
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/join-account-group-request")
class JoinAccountGroupRequestController(
    private val joinAccountGroupRequestService: JoinAccountGroupRequestService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @GetMapping("/count-received")
    fun countReceived(): ResponseEntity<ApiResponse<Map<String, Int>>> {
        val userId = requestIdentityExtractor.getClaims().id.asString()
        val result = joinAccountGroupRequestService.countReceived(userId)

        return getSuccessResponse(mapOf("count" to result))
    }

    @GetMapping("/query-receiving")
    fun queryReceiving(
        @RequestParam pageSize: Int,
        @RequestParam pageIndex: Int,
    ): ResponseEntity<ApiResponse<List<Map<String, Any?>>>> {
        val userId = requestIdentityExtractor.getClaims().id.asString()
        val result = joinAccountGroupRequestService.queryReceiving(userId, pageSize, pageIndex)

        return getSuccessResponse(result)
    }

    @PostMapping("/send")
    fun send(@Valid @RequestBody data: SendJoinAccountGroupRequestDto): ResponseEntity<ApiResponse<Any?>> {
        val receiverId = data.receiverId
        val roles = data.roles
        val claims: MyClaims = requestIdentityExtractor.getClaims()
        val senderId = claims.id.asString()
        val accountGroupId = claims.accountGroupId.asString()
        joinAccountGroupRequestService.addOrUpdate(accountGroupId, senderId, receiverId, roles)

        return getSuccessResponse()
    }

    @PostMapping("/accept")
    fun accept(@Valid @RequestBody data: IdentityDto): ResponseEntity<ApiResponse<Any?>> {
        val id = data.id.asString()
        val receiverId = requestIdentityExtractor.getClaims().id.asString()
        joinAccountGroupRequestService.accept(id, receiverId)

        return getSuccessResponse()
    }

    @PostMapping("/reject")
    fun reject(@Valid @RequestBody data: IdentityDto): ResponseEntity<ApiResponse<Any?>> {
        val id = data.id.asString()
        val receiverId = requestIdentityExtractor.getClaims().id.asString()
        joinAccountGroupRequestService.reject(id, receiverId)

        return getSuccessResponse()
    }

    @PostMapping("/delete")
    fun delete(@Valid @RequestBody data: IdentityDto): ResponseEntity<ApiResponse<Any?>> {
        val id = data.id.asString()
        val receiverId = requestIdentityExtractor.getClaims().id.asString()
        joinAccountGroupRequestService.reject(id, receiverId)

        return getSuccessResponse()
    }
}